package com.heima.article.service.Impl;

import com.alibaba.nacos.api.utils.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleFreemarkerService;
import com.heima.article.service.ApArticleService;
import com.heima.common.constant.ArticleConstant;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import java.util.Date;
import java.util.List;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    ApArticleMapper apArticleMapper;

    @Autowired
    ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    ApArticleContentMapper apArticleContentMapper;

    @Autowired
    ApArticleFreemarkerService apArticleFreemarkerService;

    /**
     * 分页查询
     * @param dto
     * @param type
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto, Short type){
        // 1 校验参数 dto
        Integer size = dto.getSize();
        // 分页的值不能超过最小值
        if(size == null || size <= 0){
            dto.setSize(10);
        }else{
            dto.setSize(Math.min(size,ArticleConstant.MIN_NUM));
        }

        // 2 校验 type
        if( !type.equals(ArticleConstant.LOAD_MORE) && !type.equals(ArticleConstant.LOAD_NEW) ){
            type = ArticleConstant.LOAD_MORE;
        }

        //文章频道校验
        if(StringUtils.isEmpty(dto.getTag())){
            dto.setTag(ArticleConstant.DEFAULT_TAG);
        }

        // 3 时间校验
        if(dto.getMaxBehotTime() == null){
            dto.setMaxBehotTime(new Date());
        }
        if(dto.getMinBehotTime() == null){
            dto.setMinBehotTime(new Date());
        }

        // 4 查询
        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, type);

        // 5 返回结果
        return ResponseResult.okResult(apArticles);

    }


    /**
     * 加载文章
     * @param dto
     * @return
     */
    public ResponseResult saveArtilce(ArticleDto dto){
        // 1 检验参数
        if(dto == null || dto.getContent() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);
        // 2 保存或者修改
        if(dto.getId() == null){
            // 2.1 保存要保存文章,文章内容,文章配置

            // 保存文章
            save(apArticle);

            // 保存配置
            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
            apArticleConfigMapper.insert(apArticleConfig);

            // 保存文章内容
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(apArticleContent);

        }else{
            // 2.2 修改只需要修改文章和文章内容

            // 修改文章
            updateById(apArticle);

            // 修改文章内容
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers
                    .<ApArticleContent>lambdaQuery()
                    .eq(ApArticleContent::getArticleId, dto.getId()));
            if(apArticleContent == null){
                return ResponseResult.errorResult(AppHttpCodeEnum.ARTICLE_NOT_EXIST);
            }
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(apArticleContent);

        }

        // 将保存的内容转为静态文件上传到minio中
        apArticleFreemarkerService.buildArticleToMinIO(apArticle,dto.getContent());

        // 3 返回结果
        return ResponseResult.okResult(apArticle.getId());

    }

}
